{layout name="public/layout" /}
{include file='yxnavbar'}


<div class="main">
    {include file='../../plugin/lottery/template/web/lottery_common_nav'}
    {if $method == "flag_main"}
    <div class="panel panel-info">
        <div class="panel-heading">当前数量： {$page_total}</div>
        <div class="panel-body">
            <form id = "myform" action="./index.php" method="get" class="form-horizontal" role="form">
                <input type="hidden" name="c" value="site" />
                <input type="hidden" name="a" value="entry" />
                <input type="hidden" name="m" value="{$_P['m']}" />
                <input type="hidden" name="do" value="web" />
                <input type="hidden" name="r" value="lottery.flag.search" />
                <input type="hidden" name="status" value="{$status}">
                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">奖项类型</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $type_arr $row}
                            <a href="{php echo createWebUrlPlugin('lottery.flag',array('themeid'=>$themeid,'status'=>$row['status'],'p_id'=>$p_id));}" class="btn {if $status == $row['status']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>

                    </div>
                    {if count($themelist)>1 }
                    <div class="col-xs-12 col-sm-2 col-md-2">
                        <select onchange="submitForm();" class="form-control" name="themeid">
                            {loop $themelist $row}
                            <option value="{$row['id']}" {if $themeid == $row['id'] }selected="selected"{/if}>{$row['id']} - {php echo mb_substr($row['title'],0,8,'utf-8');}</option>
                            {/loop}
                        </select>
                    </div>
                    {else}
                    <input type="hidden" name="themeid" value="{$themeid}" />
                    {/if}
                </div>
                <div class="form-group">
                    <label class="col-xs-1 col-sm-1 col-md-1 control-label">奖品</label>
                    <div class="col-xs-11 col-sm-9 col-md-9">
                        <div class="btn-group">
                            {loop $prize_arr $row}
                            <a href="{php echo createWebUrlPlugin('lottery.flag',array('themeid'=>$themeid,'p_id'=>$row['p_id'],'status'=>$status));}" class="btn {if $p_id == $row['p_id']}btn-primary{else}btn-default{/if}">
                                {$row['title']}
                                <span style="color: #ff0000">{$row['total']}</span>
                            </a>
                            {/loop}
                        </div>

                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">中奖时间</label>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        {php echo view_form_field_daterange('createtime', array('start'=>date('Y-m-d H:i:s',$starttime),'end'=>date('Y-m-d H:i:s',$endtime)), true);}
                    </div>
                    {if $status ==-1}
                    <div class="col-sm-2 col-lg-2 col-md-2 col-xs-2" style="margin-left: .1rem;">
                        <input class="btn btn-default" type="button" onclick="changeOrDelItem(this)" data-type="del_time"
                               data-edit-url="{php echo createWebUrlPlugin('lottery.flag.del');}"
                               data-themeid="{$themeid}"   value="删除该时间端内记录">
                    </div>
                    {/if}
                </div>
                <div class="form-group clearfix">
                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">关键字</label>
                    <div class="col-sm-3 col-xs-3 col-md-3">
                        <input class="form-control" name="search_data" placeholder="输入报名手机号或微信昵称" type="text" value="{$_P['search_data']}">
                    </div>
                    {if $can_del_ids}
                    <input type="hidden" name="can_del_ids" id="can_del_ids" value="{$can_del_ids}">
                    {/if}
                    {if $status ==-1 && $search_data}
                    <div class="col-sm-2 col-lg-2 col-md-2 col-xs-2" style="margin-left: .1rem;">
                        <input class="btn btn-default" type="button" onclick="changeOrDelItem(this)" data-type="del_search"
                               data-edit-url="{php echo createWebUrlPlugin('lottery.flag.del');}"
                               data-themeid="{$themeid}"   value="删除该搜索记录">
                    </div>
                    {/if}
                    <div class="col-sm-3 col-lg-1 col-md-3 col-xs-6">
                        <button class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
                        <input type="hidden" name="__token__" value="{$Request.token}"/>
                    </div>

<!--                    <div class="col-sm-2 col-lg-2 col-md-2 col-xs-2" style="margin-left: .1rem;">-->
<!--                            <input class="btn btn-default" type="button" name="virtual" id="virtual" value="增加虚拟数据">-->
<!--                    </div>-->
                    <div class="col-sm-2 col-lg-2 col-md-2 col-xs-2">
                        {if $total<=5000}
                        <input class="btn btn-default" type="submit" name="export_submit" onclick="jump()" id="export_submit" value="导出中奖记录EXCEL">
                        {else}
                        <input class="btn btn-default" type="submit" name="export_submit" onclick="jump()" id="export_submit" value="导出中奖记录EXCEL(1-5000条)">
                        {/if}
                    </div>
                    <div class="col-sm-3 col-lg-1 col-md-3 col-xs-3">
                        {if $total<=5000}
                        <input class="btn btn-default" type="submit" name="export_fans_submit" onclick="jump()" id="export_fans_submit" value="导出客资EXCEL">
                        {else}
                        <input class="btn btn-default" type="submit" name="export_fans_submit" onclick="jump()" id="export_fans_submit" value="导出客资EXCEL(1-5000条)">
                        {/if}
                    </div>
                </div>
            </form>
        </div>
    </div>
    {/if}

    <div class="tab-content">
        <div class="panel panel-default">
            <div class="panel-body table-responsive">
                <table class="table table-hover">
                    <thead class="navbar-inner">
                    <tr>
                        <th style="width: 50px">序号</th>
                        <th style="width: 120px">昵称</th>
                        <th style="width: 120px">姓名/电话</th>
<!--                        <th style="width: 100px">奖项名称</th>-->
                        <th style="width: 100px">奖品名称</th>
                        <th style="width: 100px">奖品图片</th>
                        <th style="width: 100px">定单状态</th>
                        <th style="width: 100px">状态</th>
                        <th style="width: 100px">核销人</th>
                        <th style="width: 100px">中奖时间</th>
                        <th style="width: 100px;text-align: right">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {loop $list $index $item}
                    <tr>
                        <td>{$item['f_id']}</td>
                        <td>
                            <img src="{php echo tomedia($item['avatar']);}" style="width: 28px;height: 28px;padding: 1px;">
                            <br>
                            {$item['nickname']}
                        </td>
                        <td>{$item['username']}<br/>{$item['mobile']}</td>
<!--                        <td>{$item['name']}</td>-->
                        <td>{$item['title']}</td>
                        <td>
                            <img src="{php echo tomedia($item['small_img']);}" style="width: 40px;height: 40px;padding: 1px;">
                        </td>
                        <td>
                            {if $item['pay_status'] && ($item['pay_status']==1 || $item['pay_status']==3)}
                                {if $item['pay_status']==1}
                                <span class="label label-success">已支付</span>
                                {else}
                            <span class="label label-info">线下订单</span>
                                {/if}
                            {else}
                            <span class="label label-warning">未支付</span>
                            {/if}
                        </td>
                        <td>
                            {if $item['default_type']!=2 && $item['from_type']=='default'}
                            {if $item['status']==1}
                            <span class="label label-warning">待领取</span>
                            {elseif $item['status']==2}
                            <span class="label label-default">已兑换其他，当前失效</span>
                            {elseif $item['status']==3}
                            <span class="label label-danger">待核销</span>
                            {elseif $item['status']==4}
                            <span class="label label-success">已核销</span>
                            {else}
                            <span class="label label-info">虚拟数据无需操作</span>
                            {/if}
                            {else}
                            {/if}
                        </td>
                        <td>
                            {if $item['status']==4}
                            <a>
                                {if $item['workername']=='1'}
                                后台管理员
                                <br>
                                {else}
                                {$item['workername']}
                                <br>
                                {$item['teamname']}
                                <br>
                                {$item['companyname']}
                                <br>
                                {/if}
                                {$item['checked_at']}
                            </a>
                            {/if}
                        </td>
                        <td>
                            {$item['created_at']}
                        </td>

                        <td style="text-align: right">
                            {if $item['from_type']=='default' && $item['default_type']!=2}
                            {if $item['status']==1 || $item['status']==3}
                            <a class="btn btn-primary btn-sm" style="width: 35px" onclick="changeOrDelItem(this)" data-type="convert"
                               data-edit-url="{php echo createWebUrlPlugin('lottery.flag.convert');}" data-uid="{$item['uid']}" data-pid="{$item['p_id']}" data-themeid="{$themeid}" data-id="{$item['f_id']}" title="核销奖品" data-toggle="tooltip" data-placement="top"><i class="fa fa-money"> </i></a>
                            {/if}
                            {/if}
                            {if $item['status']==-1}
                            <a class="btn btn-primary btn-sm" style="width: 35px" onclick="changeOrDelItem(this)" data-type="del"
                               data-edit-url="{php echo createWebUrlPlugin('lottery.flag.del');}" data-uid="{$item['uid']}" data-pid="{$item['p_id']}"
                               data-themeid="{$themeid}" data-id="{$item['f_id']}" title="删除记录" data-toggle="tooltip" data-placement="top"><i class="fa fa-trash-o"> </i></a>
                            {/if}
                            {if $_G['isSuperAdmin'] && $item['status']!=-1}
                            <a class="btn btn-success btn-sm" style="width: 35px" onclick="editFlagPrize(this)" data-type="edit"
                               data-edit-url="{php echo createWebUrlPlugin('lottery.flag.edit',array('themeid'=>$themeid));}"
                               data-themeid="{$themeid}" data-id="{$item['f_id']}"  title="修改奖品信息" data-toggle="tooltip" data-placement="top"><i class="fa fa-pencil-square-o"> </i></a>

                            <a class="btn btn-danger btn-sm" style="width: 35px" onclick="changeOrDelItem(this)" data-type="del"
                               data-edit-url="{php echo createWebUrlPlugin('lottery.flag.del');}" data-uid="{$item['uid']}" data-pid="{$item['p_id']}"
                               data-themeid="{$themeid}" data-id="{$item['f_id']}" title="删除记录" data-toggle="tooltip" data-placement="top"><i class="fa fa-trash-o"> </i></a>
                            {/if}
                        </td>

                    </tr>
                    {/loop}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

{$pager}
<!--增加虚拟数据弹屏-->
<div class="virtual" style="width: 40vw;height: 40vh;z-index:10;display:none;position: fixed;top: 50%;left: 50%;transform: translate(-50%,-50%) ">
    <div class="panel panel-info">
        <div class="panel-heading" style="background-color: #428bca;color: #f7f7f7;">
            <span>增加虚拟中奖记录</span>
            <span class="close_virtual" style="float: right;">关闭</span>
        </div>
        <div class="panel-body">
            <form id ="myform1"  class="form-horizontal" role="form" style="margin: 0 auto;">
                <div class="form-group" style="margin-left: 0px;">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">虚拟奖项</label>
                    <div class="col-sm-10  col-xs-12">
                        {loop $prize_arr $index $row}
                        {if $index>0}
                        <label class="radio-inline">
                            <input name="virtual_pids[]" type="checkbox" value="{$row['p_id']}" {if $index>0} checked{/if}> {$row['title']}
                        </label>
                        {/if}
                        {/loop}
                    </div>
                </div>
                <div class="form-group" style="margin-left: 0px;">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">虚拟中奖时间</label>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        {php echo view_form_field_daterange('virtualtime', array('start'=>date('Y-m-d H:i:s',time()),'end'=>date('Y-m-d H:i:s',$end_time)), true);}
                    </div>
                </div>
                <div class="form-group" style="margin-left: 0px;">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">虚拟中奖数量</label>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <input class="form-control" name="virtual_num" id="virtual_num" placeholder="输入虚拟中奖数量" type="number" value="">
                    </div>
                </div>

                <div class="form-group" id="sub_upload06" >
                    <div class=" col-sm-12">
                        <input  onclick="addVirtualData()"  type="button" value="提交" class="btn btn-primary col-lg-2">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<!--修改中奖奖品-->
<div class="edit" style="width: 40vw;height: 40vh;z-index:10;display:none;position: fixed;top: 50%;left: 50%;transform: translate(-50%,-50%) ">
    <div class="panel panel-info">
        <div class="panel-heading" style="background-color: #428bca;color: #f7f7f7;">
            <span>修改中奖记录</span>
            <span class="close_edit" style="float: right;">关闭</span>
        </div>
        <div class="panel-body">
            <form id ="myform2"  class="form-horizontal" role="form" style="margin: 0 auto;">
                <div class="form-group" style="margin-left: 0px;">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label">选择修改奖项</label>
                    <div class="col-sm-10  col-xs-12">
                        {loop $can_edit_list  $row}
                        <label class="radio-inline">
                            <input name="edit_pid" type="radio" value="{$row['p_id']}" > {$row['title']}
                        </label>
                        {/loop}
                    </div>
                    <input type="hidden" id="flag_id" name="flag_id" value="">
                </div>

                <div class="form-group" id="sub_upload07" >
                    <div class=" col-sm-12">
                        <input  onclick="subEditFlagPrize()"  type="button" value="提交" class="btn btn-primary col-lg-2">
                    </div>
                </div>            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(
        function (){
            //检车后台设置未中奖奖品
            // let has_free = $("#has_free").val();
            // if (!has_free){
            //     layer.msg('后台未设置未中奖奖品');
            // }
        }
    );
    function jump(){
        layer.msg('正在准备下载');
    }
    $("#virtual").click(function () {
        let total_show_pr = "{$total_show_pr}";
        if (total_show_pr<=0){
            layer.msg('奖项列表中奖记录占比设置不合法，虚拟奖项的中奖记录占比需大于0');
            return;
        }
        $('.virtual').show();
    })
    $('.close_virtual').click(function () {
        $('.virtual').hide();
    });
    function submitForm(){
        var form = document.getElementById("myform");
        form.submit();
    }
    // 修改、删除
    function changeOrDelItem(e) {
        let type = e.getAttribute("data-type");
        let themeid = e.getAttribute("data-themeid");
        let postUrl =e.getAttribute("data-edit-url");
        if (type=='convert'){
            let f_id = e.getAttribute("data-id");
            let p_id = e.getAttribute("data-pid");
            let uid = e.getAttribute("data-uid");
            layer.msg('您确定核销该奖品吗？',{
                btn:['确定','取消'],
                yes:function (index) {
                    $.ajax({
                        url:postUrl,
                        dataType:'json',
                        method:'post',
                        data: {f_id:f_id,themeid:themeid,uid:uid,p_id:p_id},
                        success:function (e) {
                            let res = e.result;
                            let status = res.status;
                            let msg = res.msg;
                            if (status!=1){
                                layer.msg(msg);
                            }else{
                                layer.msg(msg);
                            }
                            window.parent.location.reload();
                        },
                        fail:function (err) {
                            console.log(err)
                        }
                    });

                }
            });
        }
        if (type=='del'){
            let f_id = e.getAttribute("data-id");
            layer.msg('您确定删除该记录吗？',{
                btn:['确定','取消'],
                yes:function (index) {
                    $.ajax({
                        url:postUrl,
                        dataType:'json',
                        method:'post',
                        data: {f_id:f_id,themeid:themeid,type:type},
                        success:function (e) {
                            let res = e.result;
                            let status = res.status;
                            let msg = res.msg;
                            if (status!=1){
                                layer.msg(msg);
                            }else{
                                layer.msg(msg);
                            }
                            window.parent.location.reload();
                        },
                        fail:function (err) {
                            console.log(err)
                        }
                    });

                }
            });
        }
        if (type=='del_time'){
            let del_start = $("[name='createtime[start]']").val();
            let del_end = $("[name='createtime[end]']").val();
            layer.msg('您确定删除记录吗？',{
                btn:['确定','取消'],
                yes:function (index) {
                    $.ajax({
                        url:postUrl,
                        dataType:'json',
                        method:'post',
                        data: {del_start:del_start,del_end:del_end,themeid:themeid,type:type},
                        success:function (e) {
                            let res = e.result;
                            let status = res.status;
                            let msg = res.msg;
                            if (status!=1){
                                layer.msg(msg);
                            }else{
                                layer.msg(msg);
                            }
                            window.parent.location.reload();
                        },
                        fail:function (err) {
                            console.log(err)
                        }
                    });

                }
            });
        }
        if (type=='del_search'){
            let del_search = $("#can_del_ids").val();
            layer.msg('您确定删除记录吗？',{
                btn:['确定','取消'],
                yes:function (index) {
                    $.ajax({
                        url:postUrl,
                        dataType:'json',
                        method:'post',
                        data: {del_search:del_search,themeid:themeid,type:type},
                        success:function (e) {
                            let res = e.result;
                            let status = res.status;
                            let msg = res.msg;
                            if (status!=1){
                                layer.msg(msg);
                            }else{
                                layer.msg(msg);
                            }
                            window.parent.location.reload();
                        },
                        fail:function (err) {
                            console.log(err)
                        }
                    });

                }
            });
        }
    }

    function addVirtualData()
    {
        let postUrl = "{php echo createWebUrlPlugin('lottery.flag.virtual',array('themeid'=>$themeid));}";
        let formData = $("#myform1").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    location.reload();
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }


    //增加修改中奖记录
    function editFlagPrize(e) {
        let f_id = e.getAttribute("data-id");
        $("#flag_id").val(f_id);
        $('.edit').show();
    }
    $('.close_edit').click(function () {
        $('.edit').hide();
    });

    function subEditFlagPrize()
    {
        let postUrl = "{php echo createWebUrlPlugin('lottery.flag.edit',array('themeid'=>$themeid));}";
        let formData = $("#myform2").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg);
                    location.reload();
                }else{
                    layer.confirm(result.msg,{title:'提醒',icon:'2'});
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }
</script>
